<template>
  <div class="app-container">
    <div class="grid-content ep-bg-purple" />
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="开展日期" style="width: 308px">
        <el-date-picker v-model="daterangeSendTime" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期"
          end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain @click="handleAdd" v-hasPermi="['coordination:class:add']">添加班组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['coordination:class:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="10">

        <!-- <div class="ul">
          <div class="list" :class="item.classId==selectId?'active':''" v-for="(item,index) in offcialList" :key="index"
            @click.stop="handleCurrentChange(item.classId)">
            <div class="flex between">
              <div class="flex">
                <div class="serice">{{index+1}}</div>

                <div @click.stop="previewFun(item.classPlan)" class="name">{{item.className}}</div>
              </div>

              <div class="flex">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(item)" v-hasPermi="['coordination:class:edit']"></el-button>
                <el-button link type="primary" icon="Delete" @click="handleDelete(item)" v-hasPermi="['coordination:class:remove']"></el-button>
              </div>
            </div>
        
            <div class="absolute">
              <span class="text-theme">{{item.classNum}}</span>
            </div>
            <div class="demand">添加时间：{{ parseTime(item.createTime, '{y}-{m}-{d}') }}</div>

          </div>
        </div> -->

        <el-table :data="offcialList" style="width: 100%" show-overflow-tooltip   highlight-current-row @current-change="handleCurrentChange">
          <el-table-column prop="deptName" label="收费站" />
          <el-table-column prop="className" label="班组名称"  />
          <el-table-column prop="classNum" label="班组人数" />
          <el-table-column prop="classPlan" label="班组建设制度">
            <template #default="scope">
              <el-button @click.stop="previewFun(scope.row.classPlan)" v-if="scope.row.classPlan">查看</el-button>
              <span v-else>暂无数据</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" prop="" width="200">
            <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['coordination:class:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['coordination:class:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
      </el-col>
      <el-col :span="14">
        <div class="grid-content ep-bg-purple"></div>
        <!-- <el-radio-group v-model="radioType" @change="bindChangeType">
          <el-radio-button label="班组例会" value="1" />
          <el-radio-button label="班组活动" value="2" />
        </el-radio-group> -->

        <el-tabs v-model="radioType" class="demo-tabs" @tab-change="bindChangeType">
          <el-tab-pane label="班组例会" name="班组例会"></el-tab-pane>
          <el-tab-pane label="班组活动" name="班组活动"></el-tab-pane>
        </el-tabs>
        <div class="empty-top"></div>
        <div v-if="radioType=='班组例会'">
          <el-button type="primary" class="margin-bottom" @click="addMeeting(1)" v-hasPermi="['coordination:classActive:add']">新增</el-button>
          <el-table v-loading="loading" :data="implementList" show-overflow-tooltip height="610px"  >
            <el-table-column type="index" width="65" label="序号" align="center" />
            <el-table-column label="开展日期" align="center" prop="carryTime" />
            <el-table-column label="会议主题" align="center" prop="topic" />
            <el-table-column label="会议地点" align="center" prop="address" />
            <el-table-column label="班组例会记录" align="center" prop="record">
              <template #default="scope">
                <el-button @click.stop="previewFun(scope.row.record)" v-if="scope.row.record">查看</el-button>
                <span v-else>暂无数据</span>
              </template>
            </el-table-column>
            <el-table-column label="上传时间" align="center" prop="reportTime" />
            <el-table-column label="上传人" align="center" prop="reporterName" width="180" />
            <el-table-column label="操作" align="center" prop="" width="200">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdateMeet(scope.row)" v-hasPermi="['coordination:classActive:edit']">修改</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDeleteMeet(scope.row)"
                  v-hasPermi="['coordination:classActive:remove']">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="radioType=='班组活动'">
          <el-button type="primary" class="margin-bottom" @click="addMeeting(2)" v-hasPermi="['coordination:classActive:add']">新增</el-button>
          <el-table v-loading="loading" :data="implementList" show-overflow-tooltip height="610px">
            <el-table-column type="index" width="65" label="序号" align="center" />
            <el-table-column label="活动时间" align="center" prop="carryTime" />
            <el-table-column label="活动主题" align="center" prop="topic" />
            <el-table-column label="活动地点" align="center" prop="address" />

            <el-table-column label="班组活动记录表" align="center" prop="record">
              <template #default="scope">
                <el-button @click.stop="previewFun(scope.row.record)" v-if="scope.row.record">查看</el-button>
                <span v-else>暂无数据</span>
              </template>
            </el-table-column>
            <el-table-column label="班组活动总结" align="center" prop="summary">
              <template #default="scope">
                <el-button @click.stop="previewFun(scope.row.summary)" v-if="scope.row.summary">查看</el-button>
                <span v-else>暂无数据</span>
              </template>
            </el-table-column>
            <el-table-column label="活动照片" align="center" prop="img">
              <template #default="scope">
                <image-preview :src="scope.row.imgUrl" :width="50" :height="50" />
              </template></el-table-column>
            <el-table-column label="上传人" align="center" prop="reporterName" width="180" />
            <el-table-column label="上传时间" align="center" prop="reportTime" width="180" />

            <el-table-column label="操作" align="center" prop="" width="200">
              <template #default="scope">
                <el-button link type="primary" icon="Edit" @click="handleUpdateMeet(scope.row)" v-hasPermi="['coordination:classActive:edit']">修改</el-button>
                <el-button link type="primary" icon="Delete" @click="handleDeleteMeet(scope.row)"
                  v-hasPermi="['coordination:classActive:remove']">删除</el-button>
              </template>

            </el-table-column>

          </el-table>
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改班组对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="offcialRef" :model="form" :rules="rules" label-width="120px">

        <!-- 
        <el-form-item label="下发时间" prop="sendTime">
          <el-date-picker style="width:100%" clearable v-model="form.sendTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择下发时间">
          </el-date-picker>
        </el-form-item> -->
        <!-- <el-form-item label="收费站" prop="deptIds">
          <div>
            <el-radio-group v-model="radioDept" size="large" class="margin-bottom">
              <el-radio-button label="全部收费站" :value="1" />
              <el-radio-button label="部分收费站" :value="2" />
            </el-radio-group>
            <el-transfer v-model="form.deptIds" v-if="radioDept=='部分收费站'" :data="deptOptions" :titles="['收费站', '重点工作下发站']" />
          </div>
        </el-form-item> -->
        <el-form-item label="收费站" prop="deptId">
          <el-tree-select style="width: 100%;" v-model="form.deptId" :data="deptOptions" :props="{ value: 'id', label: 'label', children: 'children' }"
            value-key="id" placeholder="请选择" check-strictly />
        </el-form-item>
        <el-form-item label="班组名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="班组人数" prop="classNum">
          <el-input v-model="form.classNum" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="班组建设制度" prop="classPlan">
          <FileUpload :modelValue="form.classPlan" v-model="form.classPlan" />
        </el-form-item>
        <!-- <el-form-item label="附件" prop="fileUrl">
          <FileUpload :modelValue="form.fileUrl" v-model="form.fileUrl" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="title" v-model="dialog" width="800px" append-to-body>
      <el-form ref="meetRef" :model="formMeet" :rules="rules" label-width="120px">

        <el-form-item :label="radioType=='班组例会'?'开展日期':'活动时间'" prop="carryTime">
          <el-date-picker style="width: 100%;" v-model="formMeet.carryTime" value-format="YYYY-MM-DD" type="date" placeholder="开始日期"></el-date-picker>
        </el-form-item>
        <el-form-item :label="radioType=='班组例会'?'会议主题':'活动主题'" prop="topic">
          <el-input v-model="formMeet.topic" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="radioType=='班组例会'?'会议地点':'活动地点'" prop="address">
          <el-input v-model="formMeet.address" placeholder="请输入" />
        </el-form-item>
        <el-form-item :label="radioType=='班组例会'?'班组例会记录':'班组活动记录'" prop="record">
          <FileUpload :modelValue="formMeet.record" v-model="formMeet.record" />
        </el-form-item>
        <el-form-item label="班组活动总结" prop="summary" v-if="radioType=='班组活动'">
          <FileUpload :modelValue="formMeet.summary" v-model="formMeet.summary" />
        </el-form-item>
        <el-form-item label="活动照片" prop="img" v-if="radioType=='班组活动'">
          <image-upload v-model="formMeet.img" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormMeet">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <Viewer ref="fileViewerRef" :dialog-doc="dialogDoc" />
  </div>
</template>

<script setup name="team">
import { listClass, getClass, delClass, addClass, updateClass } from "@/api/coordination/team";
import { listClassActive, getClassActive, delClassActive, addClassActive, updateClassActive } from "@/api/coordination/class";
import { deptTreeSelect } from "@/api/system/user";
import { getCKInfo } from '@/utils/auth'
import Viewer from "@/components/Custom/viewer.vue";


const { proxy } = getCurrentInstance();
const { coo_impl_type, coo_official_source } = proxy.useDict('coo_impl_type', 'coo_official_source');
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const radioType = ref('班组例会')
const offcialList = ref([]);
const fileViewerRef = ref()
const tbCooOfficialImplList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedTbCooOfficialImpl = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeSendTime = ref([]);
const implementList = ref([])
const selectId = ref()
const deptOptions = ref([])
const deptId = ref(Number(getCKInfo('deptId')))
const dialogDoc = ref(false)
const dialog = ref(false)

const data = reactive({
  form: { deptId: deptId.value },
  formMeet: {},
  queryParams: {
    pageNum: 1,
    pageSize: 30,
  },
  classqueryParams: {
    pageNum: 1,
    pageSize: 10,
    pageType: 1,
  },
  activequeryParams: {
    pageNum: 1,
    pageSize: 10,
    pageType: 2
  },
  rules: {
  }
});

const { queryParams, form, rules, classqueryParams, activequeryParams, formMeet } = toRefs(data);

/** 查询班组列表 */
function getList() {
  loading.value = true;
  listClass(queryParams.value).then(response => {
    offcialList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function bindChangeType(val) {

  radioType.value = val
  getImplementList()

}

/** 查询落实管理列表 */
function getImplementList() {
  if (!selectId.value) return;
  loading.value = true;
  listClassActive({ ...classqueryParams.value, classId: selectId.value, pageType: radioType.value == '班组例会' ? 1 : 2 }).then(response => {
    implementList.value = response.rows;
    loading.value = false;
  });
}
/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then(response => {
    deptOptions.value = response.data;
  });
};
// 取消按钮
function cancel() {
  open.value = false;
  dialog.value = false;
  reset();
}

const handleCurrentChange = (data) => {
  // console.log(555,id)
  selectId.value = data.classId;
  getImplementList();
}

// 表单重置
function reset() {
  form.value = {
    deptId: deptId.value
  };
  formMeet.value = {}
  tbCooOfficialImplList.value = [];
  proxy.resetForm("offcialRef");
  proxy.resetForm("meetRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeSendTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

const addMeeting = (type) => {
  if (!selectId.value) {
    proxy.$modal.msgError("您没有选中责任班组");
    return
  }
  reset();
  dialog.value = true;
  title.value = type == 1 ? "添加班组例会" : "添加班组活动";
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加班组";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.classId || ids.value
  getClass(_id).then(response => {
    form.value = response.data;
    tbCooOfficialImplList.value = response.data.tbCooOfficialImplList;
    open.value = true;
    title.value = "修改班组";
  });
}
/** 修改按钮操作 */
function handleUpdateMeet(row) {
  reset();
  const _id = row.id || ids.value
  getClassActive(_id).then(response => {
    form.value = response.data;
    tbCooOfficialImplList.value = response.data.tbCooOfficialImplList;
    dialog.value = true;
    title.value = radioType.value == '班组例会' ? "修改班组例会" : "修改班组活动";
  });
}


/** 提交按钮 */
function submitForm() {
  proxy.$refs["offcialRef"].validate(valid => {
    if (valid) {

      if (form.value.id != null) {
        updateClass(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addClass(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function submitFormMeet() {
  proxy.$refs["meetRef"].validate(valid => {
    if (valid) {
      formMeet.value.pageType = radioType.value == '班组例会' ? 1 : 2;
      formMeet.value.classId = selectId.value;
      if (formMeet.value.id != null) {
        updateClassActive(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          dialog.value = false;
          getImplementList();
        });
      } else {
        addClassActive(formMeet.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          dialog.value = false;
          getImplementList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.classId || ids.value;
  proxy.$modal.confirm('是否确认删除班组编号为"' + _ids + '"的数据项？').then(function () {
    return delClass(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 删除按钮操作 */
function handleDeleteMeet(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除编号为"' + _ids + '"的数据项？').then(function () {
    return delClassActive(_ids);
  }).then(() => {
    getImplementList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 重点工作落实情况上报序号 */
function rowTbCooOfficialImplIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 重点工作落实情况上报添加按钮操作 */
function handleAddTbCooOfficialImpl() {
  let obj = {};
  obj.deptId = "";
  obj.implType = "";
  obj.implDescribe = "";
  obj.proof = "";
  obj.certifier = "";
  obj.status = "";
  tbCooOfficialImplList.value.push(obj);
}

/** 重点工作落实情况上报删除按钮操作 */
function handleDeleteTbCooOfficialImpl() {
  if (checkedTbCooOfficialImpl.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的重点工作落实情况上报数据");
  } else {
    const tbCooOfficialImpls = tbCooOfficialImplList.value;
    const checkedTbCooOfficialImpls = checkedTbCooOfficialImpl.value;
    tbCooOfficialImplList.value = tbCooOfficialImpls.filter(function (item) {
      return checkedTbCooOfficialImpls.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleTbCooOfficialImplSelectionChange(selection) {
  checkedTbCooOfficialImpl.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('/coordination/class/export', {
    ...queryParams.value
  }, `班组建设_${new Date().getTime()}.xlsx`)
}

// 预览
const previewFun = (file) => {
  dialogDoc.value = true;
  let data = {};
  // 上传过的文件组成完成的网络路径
  if (file&&file.indexOf(',')==-1) {
    data.src = `${baseUrl}${file}`;
  }else if(file&&file.indexOf(',')!=-1){
    let src=file.split(',');
    let srcArr=[]
    src.forEach(item=>{srcArr.push(`${baseUrl}${item}`)})
    data.src = srcArr.join(',');
    file=srcArr[0]
  }

  const arr = file.split(".");
  const suffix = arr[arr.length - 1];
  if (suffix == "docx" || suffix == "doc") {
    fileViewerRef.value?.viewDocx(data, '班组建设');
  } else if (suffix == "xlsx" || suffix == "xls") {
    fileViewerRef.value?.viewXlsx(data);
  } else if (suffix == "jpg" || suffix == "jpeg" || suffix == "png") {
    fileViewerRef.value?.viewImg(data);
  } else if (suffix == "pdf") {
    fileViewerRef.value?.viewPdf(data);
  }
};

getList()
getDeptTree()
</script>

<style scoped>
.margin-bottom {
  margin-bottom: 10px;
}
.list {
  margin: 10px;
  cursor: pointer;
  padding: 15px;
  box-sizing: border-box;
  border: 1px solid #f5f5f5;
  box-shadow: 0 0 5px #f5f5f5;
  position: relative;
  border-radius: 10px;
  border: 1px solid #fff;
}
.list .flex {
  display: flex;
  align-items: center;
}
.between {
  justify-content: space-between;
  margin-bottom: 6px;
}
.list .serice {
  width: 20px;
  line-height: 20px;
  background: #246bcc;
  color: #fff;
  border-radius: 4px;
  text-align: center;
  font-size: 12px;
  margin-right: 6px;
}
.list .name {
  width: 400px;
  flex-shrink: 0;
  color: #246bcc;
  font-size: 16px;
  font-weight: 600;
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
}
.list .demand {
  font-size: 14px;
  color: #999;
  padding-left: 32px;
  line-height: 1.6;
  margin-top: 6px;
}
.list .time {
  font-size: 12px;
  color: #c7c7c7;
  line-height: 1.6;
  padding-left: 32px;
}
.list .text-theme {
  color: #246bcc;
  font-size: 30px;
}
.list .source {
  font-size: 16px;
  color: #333;
  margin-right: 6px;
}
.list .absolute {
  position: absolute;
  right: 20px;
  top: 45px;
  font-size: 22px;
  font-weight: 600;
  color: #999;
}
.empty-top {
  height: 10px;
}
.list.active {
  background: #e2eeff;
  border: 1px solid #246bcc;
}
.margin-bottom {
  margin-bottom: 10px;
}

/deep/ .el-table__body tr.current-row>td.el-table__cell{
  background-color: #2e7adb;
    color: #fff;
}
/deep/ .el-table__body tr.current-row .el-button.is-link{
  color: #fff;
}
</style>

